'use strict';

angular.module('app')
	.controller('pointOrderInfoController', [ '$rootScope', '$scope', '$http', '$state',
	                                function($rootScope, $scope, $http, $state) {
		$scope.title = '订单管理';
        $scope.param = { };
        $scope.loading = false;
        $scope.orderDeatil={};
        $scope.update={};
		$scope.search = function () {
	        $scope.loading = true;
			$.ajax({
				url : $scope.app.project_name + '/orderinfo/list',
				data: $scope.param
			}).then(function(result) {
		        $scope.loading = false;
				if (result.httpCode == 200) {
					$scope.pageInfo = result.data;
					console.log("======");
					console.log(result.data);
				} else {
					$scope.msg = result.msg;
				}
				$scope.$apply();
			});
		}
                                        $scope.searchRe = function () {
                                            $scope.loading = true;
                                            console.log( $scope.param);
                                            $scope.param.startTime = $("#startTime").val();
                                            $scope.param.endTime = $("#endTime").val();
                                            $scope.param.pageNum=1;
                                            $.ajax({
                                                url : $scope.app.project_name + '/orderinfo/list',
                                                data: $scope.param
                                            }).then(function(result) {
                                                $scope.loading = false;
                                                if (result.httpCode == 200) {
                                                    $scope.pageInfo = result.data;
                                                    console.log(result.data);
                                                } else {
                                                    $scope.msg = result.msg;
                                                }
                                                $scope.$apply();
                                            });
                                        }


        $scope.search();

        $scope.clearSearch = function() {
            $scope.param={};
           $("#startTime").val();
            $scope.search();
        }


                                        $scope.getselectLog=function(){

                                            $.ajax({
                                                url : $scope.app.project_name + '/orderinfo/getselect'
                                            }).then(function(result) {
                                                $scope.loading = false;
                                                if (result.httpCode == 200) {
                                                    $scope.logList=result.data;
                                                } else {
                                                    alert("请求失败");
                                                }
                                                $scope.$apply();
                                            });

                                        }
                                        $scope.getselectLog();
                                        $scope.approvelook = function(item){
                                            $scope.firstApply1 = true;

                                            $scope.orderDeatil=item;

                                            $scope.update.logisticsCode=$scope.orderDeatil.logisticsCode
                                            $scope.update.logisticsNo=$scope.orderDeatil.logisticsNo;
                                            $scope.update.id=   $scope.orderDeatil.id;

                                            var udpatecon=item.updateConsigneeInfo.split("|");
                                            $("#updatecon").hide();
                                            var flag1=false;
                                            var flag2=false;
                                            var flag3=false;
                                            $scope.update.consigneeName="";
                                            $scope.update.consigneePhone="";
                                            $scope.update.consigneeAddress="";
                                            for(var i in udpatecon){
                                                if(i==0){
                                                    $scope.update.consigneeName=udpatecon[i];

                                                    if($.trim(udpatecon[i])!=''){
                                                        flag1=true;
                                                    }
                                                }else if (i==1){
                                                    if($.trim(udpatecon[i])!=''){
                                                        flag2=true;
                                                    }
                                                    $scope.update.consigneePhone=udpatecon[i];
                                                }else if(i==2){
                                                    if($.trim(udpatecon[i])!=''){
                                                        flag3=true;
                                                    }
                                                    $scope.update.consigneeAddress=udpatecon[i];
                                                }
                                            }

                                            $scope.flag='N';
                                            $scope.btnflag='Y';
                                            if(flag1 || flag2 || flag3){

                                                $scope.flag='Y';
                                            }
                                            $("#updateBtn").show();
                                            if(item.status==0 ){

                                            }else {

                                                $scope.btnflag='N';
                                                $("#updatecon").hide();
                                            }

                                        }
                                        $scope.updateCon=function(){
                                            $("#updatecon").show();
                                            $("#updateBtn").hide();

                                        }
		// 翻页
        $scope.pagination = function (page) {
            $scope.param.pageNum=page;
            $scope.search();
        };
                                        $scope.updateOrder=function(){



                                                console.log($scope.update.logisticsCode);


                                                if(($scope.update.logisticsCode==undefined && $scope.update.logisticsCode=='')  &&
                                                    ($scope.update.logisticsNo!=undefined &&$scope.update.logisticsNo!='')){
                                                        alert("物流公司编号和物流单号必须同时不能为空");
                                                        return ;

                                                }

                                                if(($scope.update.logisticsCode!=undefined && $scope.update.logisticsCode!='')&&
                                                    ($scope.update.logisticsNo==undefined&& $scope.update.logisticsNo=='')){
                                                    alert("物流公司编号和物流单号必须同时不能为空");
                                                    return ;

                                                }
                                                if($scope.update.consigneeName.length>200){
                                                    alert("收货人名称过长");
                                                    return ;
                                                }
                                            if($scope.update.consigneePhone.length>11){
                                                alert("收货人手机号过长");
                                                return ;
                                            }
                                            if($scope.update.consigneeAddress.length>300){
                                                alert("收货人地址过长");
                                                return ;
                                            }

                                                var conName=$scope.update.consigneeName==undefined?"":$scope.update.consigneeName;
                                                var conphone=$scope.update.consigneePhone==undefined?"":$scope.update.consigneePhone;
                                            var conAdd=$scope.update.consigneeAddress==undefined?"":$scope.update.consigneeAddress;
                                            var updateConsigneeInfo="";
                                            if(conName!="" || conphone!="" || conphone!=""){
                                                if(conName==""||conphone=="" || conphone==""){
                                                    alert("请完善收货信息");
                                                    return ;
                                                }
                                                updateConsigneeInfo=conName+'|'+conphone+'|'+conAdd;
                                            }
                                                var orderNo=$scope.orderDeatil.orderNo;
                                            var userId=$scope.orderDeatil.userId;

                                                var param={"updateConsigneeInfo":updateConsigneeInfo,
                                                    "logisticsCode":$scope.update.logisticsCode,"logisticsNo":$scope.update.logisticsNo,
                                                    "id":$scope.update.id,"userId":userId,"orderNo":orderNo};

                                                    console.log(param);
                                                $.ajax({
                                                    url : $scope.app.project_name + '/orderinfo/update',
                                                    data: param
                                                }).then(function(result) {
                                                    $scope.loading = false;

                                                    if (result.httpCode == 200) {
                                                        $scope.firstApply1 = false;
                                                        $scope.search();
                                                    } else {
                                                        alert("请求失败");
                                                    }
                                                    $scope.$apply();
                                                });
                                        }



                                        // 初始化页面
                                        $scope.getpinfo=function (id) {


                                            $.ajax({
                                                url : $scope.app.project_name + '/productInfo/read/detail',
                                                data: {'id': id}
                                            }).then(function(result) {
                                                console.log(result);

                                                if (result.httpCode == 200) {
                                                    $scope.recordpinfo = result.data;
                                                    if(result.data!=undefined){
                                                    console.log(result.data);

                                                    $scope.firstApply2 = true;
                                                    }
                                                } else {
                                                    $scope.msg = result.msg;
                                                }
                                                $scope.$apply();
                                            });
                                        }
} ]);